import { Button, Tooltip, InputNumber, Form, message } from 'antd';
import { onMouseOutBtn, renderTableText, tableSorter } from '@/utils';
import { PlusCircleOutlined, DeleteOutlined, AppstoreAddOutlined } from '@ant-design/icons';
import { DictionaryRenderFun, formatDecimalFour } from '../../Acommon';
import { QuestionCircleOutlined } from '@ant-design/icons';
import pageStyles from '@/common/pageLayout.less';

/** 材料表格 */
const contractModalColumns = (cancelContractList, changeContractData, handleSelectContract, pageType) => {
  const isDetail: boolean = pageType == 'detail'; // 是否是查看
  let tableConfig: Record<string, any>[] = [
    {
      title: '合同编号',
      dataIndex: 'contractCode',
      key: 'contractCode',
      fixed: 'left',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'contractCode'),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '合同名称',
      dataIndex: 'contractName',
      key: 'contractName',
      fixed: 'left',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'contractName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '合同金额',
      dataIndex: 'sumInTax',
      key: 'sumInTax',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'sumInTax'),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '资金来源编号',
      dataIndex: 'projectID',
      key: 'projectID',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'projectID'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '资金来源',
      dataIndex: 'projectName',
      key: 'projectName',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'projectName'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '合同签订时间',
    //   dataIndex: 'signTime',
    //   key: 'signTime',
    //   visible: true,
    //   width: 100,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'signTime'),
    //   render: (text: number | any) => {
    //     return <span>{text ? text : '--'}</span>;
    //   },
    // },
    {
      title: '采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'procureRealName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: (
        <>
          <span>
            {'有效金额'}
            <Tooltip title={'与资产转化对应的费用'}>
              <a style={{ marginLeft: 8 }}>
                <QuestionCircleOutlined />
              </a>
            </Tooltip>
          </span>
        </>
      ),
      dataIndex: 'effectiveMoney',
      key: 'effectiveMoney',
      fixed: 'right',
      visible: true,
      width: 100,
      render: (value: number | any, record: Record<string, any>, index: number) => {
        return (
          <InputNumber
            value={value}
            bordered={false}
            style={{ width: '100%' }}
            placeholder="请输入"
            onChange={(e) => {
              if (Number(e) > Number(record.sumInTax)) {
                message.error('该有效金额不能超过该合同金额,请重新输入');
                return;
              } else {
                changeContractData({ type: 'effectiveMoney', value: e, index });
              }
            }}
            disabled={isDetail}
            precision={2}
          />
        );
      },
    },
  ];
  if (pageType != 'detail') {
    tableConfig?.push({
      title: (
        <div style={{ width: '100%', textAlign: 'center' }}>
          <Button
            type="link"
            size="small"
            icon={<AppstoreAddOutlined />}
            className="manageBtntTxtStyle"
            onMouseOut={onMouseOutBtn}
            onClick={handleSelectContract}
          >
            选择
          </Button>
        </div>
      ),
      dataIndex: 'otherBtn',
      key: 'otherBtn',
      visible: true,
      width: 100,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div className="counterpoartDelBtnStyle">
            <Button
              type="link"
              size="small"
              icon={<DeleteOutlined />}
              className={'manageBtntTxtStyle'}
              onMouseOut={onMouseOutBtn}
              onClick={() => cancelContractList(record)}
            >
              删除
            </Button>
          </div>
        );
      },
    });
  }
  return tableConfig;
};

/** 表格配置 */
const contractTableConfig = () => {
  return [
    {
      title: '合同编号',
      dataIndex: 'contractCode',
      key: 'contractCode',
      fixed: 'left',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'contractCode'),
      render: (text: string) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '合同名称',
      dataIndex: 'contractName',
      key: 'contractName',
      fixed: 'left',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'contractName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '合同金额',
      dataIndex: 'sumInTax',
      key: 'sumInTax',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'sumInTax'),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '资金来源编号',
      dataIndex: 'projectID',
      key: 'projectID',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'projectID'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '资金来源',
      dataIndex: 'projectName',
      key: 'projectName',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'projectName'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '合同签订时间',
    //   dataIndex: 'signTime',
    //   key: 'signTime',
    //   visible: true,
    //   width: 100,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'signTime'),
    //   render: (text: number | any) => {
    //     return <span>{text ? text : '--'}</span>;
    //   },
    // },
    {
      title: '采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'procureRealName'),
      render: (text: string) => renderTableText(text, true),
    },
  ];
};

export { contractTableConfig, contractModalColumns };
